// pages/share/index.js
const app = getApp();

import {
  circledynamic,
  questionList,
  circledynamicclass,
  ykDynamicList,
  ykCircledynamicclass
} from '../../utils/http';
// import WxParse from '../../utils/wxParse/wxParse';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    v: app.globalData.v, //版本
    lang: app.vSwitch(), //版本
    theme: app.cSwitch(), //主题色
    patterns: app.globalData.patterns, //是否为游客模式
    loadShow: true,
    // 分页
    page: 1,
    size: 10,
    pages: 0,
    current: 0,
    total: 0,
    noData: false,
    keyWord: '',

    // 弹窗
    show: false,
    actions: [{
        name: '新增分享',
        id: 0,
      },
      {
        name: '新增提问',
        id: 1,
      }
    ],

    // tab切换
    tabIndex: 0,
    c_tabIndex: 0,
    jiaonangIndex: 0,

    c_tabIndex0: 0,
    // 文章类型
    dynamicType: [{
      name: '文章',
      id: 1
    }, {
      name: '视频',
      id: 2
    }, {
      name: '音频',
      id: 3
    }, {
      name: '文件',
      id: 5
    }],

    // 状态
    status: [{
      name: '未完成',
      id: 1
    }, {
      name: '已完成',
      id: 2
    }],

    screenPop: false, //筛选弹窗

    chooseStatus: null, //当前状态

    listType: 1, //列表类型  1、列表  2、宫格

    tagShow: true,

    loading: true,

    cid:0,//

    showsearch:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.data.patterns ? wx.hideHomeButton() : '';
    console.log(options);
    if (options.type) {
      this.setData({
        tabIndex: parseInt(options.type)
      })
    }

    if(options.cid)
    {
      this.setData({
        cid:options.cid || 0
      })
    }
    // // 隐藏原生的tabbar
    // wx.hideTabBar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // circledynamicclass().then(r => {
    //   if (r.code == 0) {
    //     this.setData({
    //       classList: r.data
    //     })
    //     this.init();
    //   }
    // })
    setTimeout(() => {
      var query = this.createSelectorQuery();
      query.select('.top').boundingClientRect((res) => {
          console.log('top', res)
          if (res) {
            this.setData({
              topHeight: res.height,
            })
            console.log('topHeight', res.height)
          }
        })
        .exec();
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      theme: app.cSwitch(), //主题色
      patterns: app.globalData.patterns, //是否为游客模式
    })
    // this.data.patterns ? wx.hideHomeButton() : '';
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.theme.tabbarColor,
    });
    wx.setNavigationBarTitle({
      title: this.data.lang.a6,
    })
    if (app.globalData.patterns) {
      if (!this.data.d) {
        let func = app.globalData.patterns && !app.globalData.token ? ykCircledynamicclass : circledynamicclass;
        func().then(r => {
          if (r.code == 0) {
            this.setData({
              classList: r.data
            })
            this.init();
          }
        })
      }
    } else {
      app.userInfo(true).then(r => {
        if (!this.data.d) {
          circledynamicclass().then(r => {
            if (r.code == 0) {
              this.setData({
                classList: r.data
              })
              this.init();
            }
          })
        }
      })

    }
  },

  // 监听页面滚动
  onPageScroll(e) {
    // console.log(e)
    let t = this;
    if (e.scrollTop <= 0) {
      e.scrollTop = 0; //最顶部
      t.setData({
        tagShow: true
      })
    } else if (e.scrollTop > t.data.pageScroll && e.scrollTop >= t.data.topHeight) {
      t.setData({
        tagShow: false
      })
    } else {
      t.setData({
        tagShow: true
      })
    }
    t.setData({
      pageScroll: e.scrollTop
    })

  },

  // 更改列表类型
  changeListType() {
    this.setData({
      listType: this.data.listType == 1 ? 2 : 1
    })
    this.data.listType == 1 ? '' : wx.lin.renderWaterFlow(this.data.d, true);
  },

  // 点击筛选标签
  changeFlag(e) {
    console.log(e.mark)
    let t = this;
    switch (e.mark.type) {
      case '1':
        t.setData({
          ['dynamicType[' + e.mark.index + '].flag']: !t.data.dynamicType[e.mark.index].flag
        })
        break;
      case '2':
        t.setData({
          ['classList[' + e.mark.index + '].flag']: !t.data.classList[e.mark.index].flag
        })
        break;
      case '3':
        let status = t.data.status;
        t.data.status.forEach((i, index) => {
          console.log(index == e.mark.index)
          if (index == e.mark.index) {
            t.setData({
              ['status[' + e.mark.index + '].flag']: !t.data.status[e.mark.index].flag
            })
          } else {
            t.setData({
              ['status[' + index + '].flag']: false
            })
          }
        })
        t.setData({
          status
        })
        break;
    }
  },

  // 确认筛选
  sureBtn() {
    let t = this;
    let chooseCard = [];
    t.data.dynamicType.forEach(i => {
      i.chooseflag = i.flag;
      i.style = 1;
      if (i.flag) {
        chooseCard.push(i)
      }
    })
    t.data.classList.forEach(i => {
      i.chooseflag = i.flag;
      i.style = 2;
      if (i.flag) {
        chooseCard.push(i)
      }
    })
    let chooseStatus = null;
    t.data.status.forEach((i, index) => {
      i.chooseflag = i.flag;
      i.style = 3;
      if (i.flag) {
        chooseStatus = index;
        chooseCard.push(i)
      }
    })
    t.setData({
      chooseStatus,
      chooseCard,
      screenPop: false
    })
    t.init();
  },

  // 重置
  resetBtn() {
    let t = this;
    let chooseCard = [];
    t.data.dynamicType.forEach(i => {
      i.chooseflag = false;
    })
    t.data.classList.forEach(i => {
      i.chooseflag = false;
    })
    let chooseStatus = null;
    t.data.status.forEach((i, index) => {
      i.chooseflag = false;
    })
    t.setData({
      chooseStatus,
      chooseCard,
      screenPop: false
    })
    t.init();
  },

  // 更改状态
  changeStatus() {
    let t = this;
    let status = t.data.status;
    let chooseStatus = t.data.chooseStatus;
    console.log(chooseStatus);
    switch (chooseStatus) {
      case null:
        chooseStatus = 0;
        status.forEach((i, index) => {
          if (index == 0) {
            i.chooseflag = true
          } else {
            i.chooseflag = false
          }
        })
        break;
      case 0:
        chooseStatus = 1;
        status.forEach((i, index) => {
          if (index == 1) {
            i.chooseflag = true
          } else {
            i.chooseflag = false
          }
        })
        break;
      case 1:
        chooseStatus = null;
        status.forEach((i, index) => {
          i.chooseflag = false
        })
        break;

    }
    t.setData({
      status,
      chooseStatus
    })
    t.init();
  },


  // 打开筛选弹窗
  openScreenPop() {
    let dynamicType = this.data.dynamicType;
    let classList = this.data.classList;
    let status = this.data.status;
    dynamicType.forEach(i => {
      i.flag = i.chooseflag;
    })
    classList.forEach(i => {
      i.flag = i.chooseflag;
    })
    status.forEach(i => {
      i.flag = i.chooseflag;
    })
    this.setData({
      dynamicType,
      classList,
      status,
      screenPop: true
    })
  },
  // 关闭筛选弹窗
  closeScreenPop() {
    this.setData({
      screenPop: false
    })
    let dynamicType = this.data.dynamicType;
    let classList = this.data.classList;
    let status = this.data.status;
    dynamicType.forEach(i => {
      i.flag = false;
    })
    classList.forEach(i => {
      i.flag = false;
    })
    status.forEach(i => {
      i.flag = false;
    })
    this.setData({
      dynamicType,
      classList,
      status
    })
  },

  async init() {
    let t = this;
    t.setData({
      page: 1,
      pages: 0,
      current: 0,
      noData: false,
      kong: false,
      loadShow: true
    })
    await t.getMore();
    t.setData({
      loadShow: false,
      loading: false
    })
  },

  getMore() {
    let t = this;
    t.setData({
      loadShow: true
    })
    var promise = new Promise((resolve, reject) => {
      // if (t.data.pages >= t.data.current) {
      let func;
      if (parseInt(t.data.tabIndex) == 0) {
        let _d = {
          page: t.data.page,
          pageSize: t.data.size,
          keyWord: t.data.keyWord,
        }
        let dynamicTypes = [],
          dynamicClasses = [],
          status = [];
          if(t.data.cid>0)
          {
            dynamicClasses.push(t.data.cid)
          }
        t.data.dynamicType.forEach(i => {
          if (i.chooseflag) {
            dynamicTypes.push(i.id)
          }
        })
        if (t.data.classList && t.data.classList.length) {
          t.data.classList.forEach(i => {
            if (i.chooseflag) {
              dynamicClasses.push(i.id)
            }
          })
        }
        t.data.status.forEach(i => {
          if (i.chooseflag) {
            status.push(i.id)
          }
        })
        dynamicTypes.length ? _d.dynamicTypes = dynamicTypes.toString() : '';
        dynamicClasses.length ? _d.dynamicClasses = dynamicClasses.toString() : '';
        status.length ? _d.status = status.toString() : '';
        func = app.globalData.patterns && !app.globalData.token ? ykDynamicList(_d) : circledynamic(_d);
      } else if (parseInt(t.data.tabIndex) == 1) {
        func = questionList({
          page: t.data.page,
          pageSize: t.data.size,
          keyWord: t.data.keyWord,
          type: parseInt(t.data.jiaonangIndex) ? 1 : 2,
          way: parseInt(t.data.c_tabIndex) ? 2 : 1,
        })
      }
      func.then(r => {
        if (r.httpCode == 200 || r.code == 0) {
          if (t.data.tabIndex == 1) {
            r.data = parseInt(t.data.c_tabIndex) ? r.data.circleDynamicCommentPage : r.data.circleDynamicPage;
          }
          let d;
          var arr2 = [];
          if (t.data.page == 1) {
            d = [];
          } else {
            d = t.data.d;
          }
          if (r.data.records.length) {
            r.data.records.forEach(i => {
              i.createTime ? i.createTime = i.createTime.substring(0, 10) : '';
              // WxParse.wxParse('rule' + d.length, 'html', i.content, t, 0);
              i.c_tabIndex = t.data.c_tabIndex;
              i.tabIndex = t.data.tabIndex;
              d.push(i);
              arr2.push(i);
            })
            if (r.data.records.length < r.data.size) {
              t.setData({
                noData: true,
              })
            } else {
              t.setData({
                noData: false,
              })
            }
            t.setData({
              kong: false,
            })
          } else {
            t.setData({
              noData: true,
            })
            if (t.data.page == 1) {
              t.setData({
                kong: true,
              })
            }
          }
          // WxParse.wxParseTemArray("WxParseListArr", 'rule', d.length, t);
          if (t.data.listType == 2) {
            if (t.data.page == 1) {
              wx.lin.renderWaterFlow(arr2, true);
            } else {
              wx.lin.renderWaterFlow(arr2, false);
            }
          }
          t.setData({
            d,
            page: ++t.data.page,
            pages: r.data.pages,
            current: r.data.current,
            total: r.data.total
          })
        }
        t.setData({
          loadShow: false
        })
        resolve();
      })
      // } else {
      //   t.setData({
      //     noData: true
      //   })
      //   resolve();
      // }
    });
    return promise;
  },

  clear() {
    this.setData({
      keyWord: ''
    })
    this.init();
  },

  goPage(e) {
    app.message().then(r => {
      app.goPage(e.mark);
    })
  },
  onOpen() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },

  onSelect(event) {
    wx.navigateTo({
      url: '/pages/share/add/index?type=' + event.detail.id,
    })
    // if (event.detail.id == 1) {
    //   wx.navigateTo({
    //     url: '/pages/share/add/index',
    //   })
    // } else if (event.detail.id == 2) {
    //   wx.navigateTo({
    //     url: '/pages/share/add/wxIndex',
    //   })
    // }
    console.log(event.detail);
  },

  changeTab(e) {
    this.setData({
      tabIndex: e.mark.index,
      d: []
    })
    this.init();
  },

  changeTab0(e) {
    this.setData({
      c_tabIndex0: e.mark.index,
      d: []
    })
    this.init();
  },

  changeTab2(e) {
    this.setData({
      c_tabIndex: e.mark.index,
      d: []
    })
    this.init();
  },

  changeTab3(e) {
    this.setData({
      jiaonangIndex: e.mark.index,
      d: []
    })
    this.init();
  },


  showsearch:function()
  {
    this.setData({
      showsearch:!this.data.showsearch
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noData) {
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.share();
  }
})